package com.delivery.www.servlet;

import com.alibaba.fastjson.JSON;
import com.delivery.www.constant.Constants;
import com.delivery.www.po.Complaint;
import com.delivery.www.po.User;
import com.delivery.www.service.ServiceImpl.ComplaintServiceImpl;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @author 12970
 */
public class ComplaintServlet extends BaseServlet {
    ComplaintServiceImpl complaintService = new ComplaintServiceImpl();

    /**
     * 投诉
     *
     * @param req  请求
     * @param resp 响应
     * @throws IOException 异常
     */
    public void doComplaint(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        PrintWriter writer = resp.getWriter();
        User user = (User) req.getSession().getAttribute(Constants.USER_SESSION);
        String field = req.getParameter("field");
        String content = req.getParameter("content");
        String store = req.getParameter("store");
        String s = complaintService.doComplaint(user.getUserId(), content, field, store);
        writer.print(s);
    }

    /**
     * 查询投诉
     *
     * @param req  请求
     * @param resp 响应
     * @throws IOException 异常
     */
    public void selectComplaint(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        PrintWriter writer = resp.getWriter();
        List<Complaint> complaints = complaintService.getComplaint();
        writer.print(JSON.toJSONString(complaints));
    }

    /**
     * 删除投诉
     *
     * @param req  请求
     * @param resp 响应
     * @throws IOException 异常
     */
    public void deleteComplaint(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        PrintWriter writer = resp.getWriter();
        int complaintId = Integer.parseInt(req.getParameter("complaintId"));
        boolean b = complaintService.deleteComplaint(complaintId);
        if (b) {
            writer.print("ok");
        } else {
            writer.print("error");
        }
    }
}
